package ext.tianma.part.report.searchAPL.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;

import wt.iba.definition.StringDefinition;
import wt.iba.definition.TimestampDefinition;
import wt.iba.value.StringValue;
import wt.query.ArrayExpression;
import wt.query.ClassAttribute;
import wt.query.ConstantExpression;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.util.WTAttributeNameIfc;

public class APLUtil {
	
	public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 动态添加IBA_String类型搜索条件
     * @param ibaHolderClass 查询对象
     * @param qs 传递qs
     * @param maramsMap 字符串IBA集合
     * @param condition 条件
     * @param splitsign 分隔符，当condition为in时使用
     * @param isAdd 是否查询出value字段
     * @throws Exception
     * @author xucr
     * @CreateDate 2020年6月24日
     */
    public static void addIBAStringCondition(Class ibaHolderClass, QuerySpec qs, 
    		Hashtable<String, Object> maramsMap, String condition, String splitsign, boolean isAdd) throws Exception {
    	
    	Enumeration<String> eIba = maramsMap.keys();
    	while(eIba.hasMoreElements()) {
    		//PS : 如下变量不能提成公共变量，每个属性，必须要用使用自己的变量
    		int sd = qs.appendClassList(StringDefinition.class, false);
    		int sv = qs.appendClassList(StringValue.class, false);
    		
    		String iba_name = eIba.nextElement();
    		Object iba_value = maramsMap.get(iba_name);
    		
    		ClassAttribute caValue = new ClassAttribute(StringValue.class,StringValue.VALUE);
    		
    		SearchCondition sc = null;
    		if(iba_value instanceof String []) {
    			String [] ibaValue = (String[]) iba_value;
    			sc = new SearchCondition(caValue, SearchCondition.IN, new ArrayExpression(ibaValue));
    		} else {
    			String ibaValue = (String)iba_value;
    			if(condition.equals(SearchCondition.LIKE)) {
    				sc = new SearchCondition(caValue, condition, new ConstantExpression(new String("%" + ibaValue.toUpperCase() + "%")));
    			} else if(condition.equals(SearchCondition.IN)) {
    				String [] valueArrays = ibaValue.split(splitsign);
    				sc = new SearchCondition(caValue, condition, new ArrayExpression(valueArrays));
    			} else {
    				sc = new SearchCondition(caValue, condition, new ConstantExpression(new String(ibaValue.toUpperCase())));
    			}
    		}
    		
    		SearchCondition scName = new SearchCondition(StringDefinition.class,StringDefinition.NAME,SearchCondition.EQUAL,iba_name);
    		SearchCondition scDef = new SearchCondition(StringValue.class,"definitionReference.key.id", StringDefinition.class,WTAttributeNameIfc.ID_NAME);
    		SearchCondition scDoc = new SearchCondition(StringValue.class,"theIBAHolderReference.key.id",ibaHolderClass,WTAttributeNameIfc.ID_NAME);
    		
    		if(qs.getConditionCount() > 0) {
    			qs.appendAnd();
    		}
    		int p = qs.getFromClause().getPosition(ibaHolderClass);
    		
    		qs.appendWhere(sc,new int [] {sv});
    		qs.appendAnd();
    		
    		qs.appendWhere(scDoc,new int [] {sv, p});
    		qs.appendAnd();
    		
    		qs.appendWhere(scDef,new int [] {sv, sd});
    		qs.appendAnd();
    		
    		qs.appendWhere(scName,new int [] {sd});
    		
    		if(isAdd) {
    			qs.appendSelectAttribute("value", sv, false);
    		}
    	}
    }
    
    /**
     * 动态添加IBA搜索条件(升级版)
     * @param ibaHolderClass 查询对象
     * @param qs 传递qs
     * @param maramsMap 字符串IBA集合
     * @param condition 条件
     * @param splitsign 分隔符，当condition为in时使用
     * @param isAdd 是否查询出value字段
     * @param definitionClass 对应类型的definition表
     * @param valueClass 对应类型的valueClass表
     * @throws Exception
     * @author xucr
     * @CreateDate 2020年6月24日
     */
    public static void addIBAStringCondition(Class ibaHolderClass, QuerySpec qs, 
    		Hashtable<String, Object> maramsMap, String condition, String splitsign, boolean isAdd, 
    		Class definitionClass, Class valueClass) throws Exception {
    	
    	Enumeration<String> eIba = maramsMap.keys();
    	while(eIba.hasMoreElements()) {
    		//PS : 如下变量不能提成公共变量，每个属性，必须要用使用自己的变量
    		int sd = qs.appendClassList(definitionClass, false);
    		int sv = qs.appendClassList(valueClass, false);
    		
    		String iba_name = eIba.nextElement();
    		Object iba_value = maramsMap.get(iba_name);
    		
    		ClassAttribute caValue = new ClassAttribute(valueClass,StringValue.VALUE);
    		
    		SearchCondition sc = null;
    		if(iba_value instanceof String []) {
    			String [] ibaValue = (String[]) iba_value;
    			sc = new SearchCondition(caValue, SearchCondition.IN, new ArrayExpression(ibaValue));
    		} else {
    			String ibaValue = (String)iba_value;
    			if(condition.equals(SearchCondition.LIKE)) {
    				if( definitionClass.getName().equals(TimestampDefinition.class.getName()) ) {//如果是日期，特殊处理成
    					Date startDate = dateFormat.parse(ibaValue);
						Timestamp t = new Timestamp(startDate.getTime());
    					sc = new SearchCondition(caValue, condition, new ConstantExpression(t));
    				} else {
    					sc = new SearchCondition(caValue, condition, new ConstantExpression(new String("%" + ibaValue.toUpperCase() + "%")));
    				}
    				
    			} else if(condition.equals(SearchCondition.IN)) {
    				String [] valueArrays = ibaValue.split(splitsign);
    				sc = new SearchCondition(caValue, condition, new ArrayExpression(valueArrays));
    			} else if(condition.equals(SearchCondition.NOT_NULL)){
    				sc = new SearchCondition(caValue, condition, null);
    			} else {
    				sc = new SearchCondition(caValue, condition, new ConstantExpression(new String(ibaValue.toUpperCase())));
    			}
    		}
    		
    		SearchCondition scName = new SearchCondition(definitionClass,"name",SearchCondition.EQUAL,iba_name);
    		SearchCondition scDef = new SearchCondition(valueClass,"definitionReference.key.id", definitionClass ,WTAttributeNameIfc.ID_NAME);
    		SearchCondition scDoc = new SearchCondition(valueClass,"theIBAHolderReference.key.id",ibaHolderClass,WTAttributeNameIfc.ID_NAME);
    		
    		if(qs.getConditionCount() > 0) {
    			qs.appendAnd();
    		}
    		int p = qs.getFromClause().getPosition(ibaHolderClass);
    		
    		qs.appendWhere(sc,new int [] {sv});
    		qs.appendAnd();
    		
    		qs.appendWhere(scDoc,new int [] {sv, p});
    		qs.appendAnd();
    		
    		qs.appendWhere(scDef,new int [] {sv, sd});
    		qs.appendAnd();
    		
    		qs.appendWhere(scName,new int [] {sd});
    		
    		if(isAdd) {
    			qs.appendSelectAttribute("value", sv, false);
    		}
    	}
    }
}
